#include "keydelegate.h"
#include <QDebug>
#include <QString>
#include <windows.h>
#include <vkmap.h>
#include <mkmessage.h>
#include <string.h>

QMap<int, char> qtKey2winKeyMap = VKMap();
QMap<QString, int> string2qtKey = CKMap();

KeyDelegate::KeyDelegate(QObject *parent)
    : QObject(parent)
{
    m_block = 36;
    m_rate = 0.75;
}

void KeyDelegate::handleKey(QString msg, QQuickItem* item, int pressed)
{
    // 处理按键事件，一般的按钮，直接发送按下或谈起
    // caps ctrl shift alt，这四个键是按一下为按下，再按一次为弹起
    msg = msg.toLower();
    auto process = [msg](int action) {
        if (string2qtKey.contains(msg)) {
            auto qtkey = string2qtKey[msg];
            MKMessage msg((uint16_t)qtKey2winKeyMap[qtkey], action, 0, 0);
            execMKMessage(msg);
        }
    };

    if (msg == "close" && pressed == false) {
        exit(0);
    }
    if (pressed == 0) {
        if (msg == "caps") {
            process(!m_caps ? KeyPress : KeyRelease);
            setCaps(!m_caps);
        } else if (msg == "shift") {
            process(!m_shift ? KeyPress : KeyRelease);
            setShift(!m_shift);
        } else if (msg == "ctrl") {
            process(!m_ctrl ? KeyPress : KeyRelease);
            setCtrl(!m_ctrl);
        } else if (msg == "alt") {
            process(!m_alt ? KeyPress : KeyRelease);
            setAlt(!m_alt);
        } else {
            process(KeyRelease);
            item->setProperty("pressed", 0);
        }
    } else {
        if (msg == "caps") {
        } else if (msg == "shift") {
        } else if (msg == "ctrl") {
        } else if (msg == "alt") {
        }
        else {
            process(KeyPress);
            item->setProperty("pressed", 1);
        }
    }

}

void KeyDelegate::regist(QQuickItem *item)
{
    auto child = item->children()[1];
//    if (child->property("nam") == "t") {
//        child->setProperty("nam", "TT");
//    }
    QString name = child->property("nam").toString();
    m_keyMap[name] = item;
    //SetWindowLong((HWND)item->window()->winId(), GWL_EXSTYLE, GetWindowLong((HWND)item->winId(), GWL_EXSTYLE) | WS_EX_NOACTIVATE);
}

void KeyDelegate::loaded(QQuickItem *item)
{

    //SetWindowLong(item, GWL_EXSTYLE, GetWindowLong(item, GWL_EXSTYLE) | WS_EX_NOACTIVATE);
    item->setFocus(false);
    SHORT s = GetKeyState(VK_CAPITAL);
    m_keyMap["root"] = item;
    setCaps(s);
    setAlt(0);
    setShift(0);
    setCtrl(0);
}

void KeyDelegate::move(QWidget *wid, int x, int y)
{
    wid->move(wid->x() + x, wid->y() + y);
}

void KeyDelegate::setBlock(int value)
{
    m_block = value;
}

void KeyDelegate::setCaps(int value)
{
    m_keyMap["root"]->setProperty("caps", QVariant::fromValue(value));
    m_caps = value;
}

void KeyDelegate::setCtrl(int value)
{
    m_keyMap["root"]->setProperty("ctrl", QVariant::fromValue(value));
    m_ctrl = value;
}

void KeyDelegate::setAlt(int value)
{
    m_keyMap["root"]->setProperty("alt", QVariant::fromValue(value));
    m_alt = value;
}

void KeyDelegate::setShift(int value)
{
    m_keyMap["root"]->setProperty("shift", QVariant::fromValue(value));
    m_shift = value;
}

int KeyDelegate::block()
{
    return m_block;
}

float KeyDelegate::rate()
{
    return m_rate;
}
